﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.IO;

namespace AJArt.Mvc.Models
{
    #region Models

    public class Story
    {
        public string Root { get; set; }
        public string Thumbnail { get; set; }
        public string[] Photos { get; set; }
    }

    #endregion

    #region Services

    public interface ICategoryService
    {
        IEnumerable<Story> GetStories(string category);
        Story GetStoryDetail(string category, string story);
    }

    public class CategoryService : ICategoryService
    {
        public IEnumerable<Story> GetStories(string category)
        {
            var photo = HttpContext.Current.Server.MapPath("~/content/photos/" + category);
            if (!Directory.Exists(photo))
                throw new DirectoryNotFoundException(photo);

            foreach (var story in Directory.GetDirectories(photo))
            {
                var dir = new DirectoryInfo(story);
                yield return new Story
                {
                    Root = dir.Name,
                    Thumbnail = category + "/" + dir.Name + ".jpg",
                };
            }
        }


        public Story GetStoryDetail(string category, string story)
        {
            var path = HttpContext.Current.Server.MapPath("~/content/photos/" + category + "/" + story);
            if (!Directory.Exists(path))
                throw new DirectoryNotFoundException(path);

            return new Story
            {
                Photos = new DirectoryInfo(path).GetFiles().Select(p => category + "/" + story + "/" + p.Name).ToArray(),
            };
        }
    }

    #endregion
}